//
//  LoginViewController.m
//  DongChiDaChiProject1
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "UserManager.h"
#import <AVOSCloud/AVOSCloud.h>

@interface LoginViewController ()<UITextFieldDelegate>

// 用户名
@property (nonatomic, strong) UILabel *userName;
// 用户名TF
@property (nonatomic, strong) UITextField *userNameTF;
// 密码
@property (nonatomic, strong) UILabel *password;
// 密码TF
@property (nonatomic, strong) UITextField *passwordTF;
// 登陆按钮
@property (nonatomic, strong) UIButton *loginButton;
// 注册按钮
@property (nonatomic, strong) UIButton *registButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTranslucent:NO];
    [self addView];
    
    [self addBar];
}

#pragma mark - 添加bar
- (void)addBar
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(returnToLast:)];
    leftItem.tintColor = [UIColor colorWithHue:0.8 saturation:1 brightness:0.7 alpha:1];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self.navigationController.navigationBar setTranslucent:NO];
}

- (void)returnToLast:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加视图
- (void)addView
{
    self.userName = [[UILabel alloc] initWithFrame:CGRectMake(40 * kWidthFitParameter, 30 * kWidthFitParameter, 80 * kWidthFitParameter, 30 * kWidthFitParameter)];
    self.userName.text = @"用户名";
  //  self.userName.backgroundColor = [UIColor redColor];
    self.userName.font = [UIFont systemFontOfSize:20 * kWidthFitParameter];
    [self.view addSubview:_userName];
    
    self.userNameTF = [[UITextField alloc] initWithFrame:CGRectMake(self.userName.right + 10 * kWidthFitParameter, self.userName.top, 200 * kWidthFitParameter, 30 * kWidthFitParameter)];
   // self.userNameTF.backgroundColor = [UIColor redColor];
    self.userNameTF.borderStyle = UITextBorderStyleRoundedRect;
    self.userNameTF.placeholder = @"请输入用户名";
    [self.view addSubview:_userNameTF];
    
    
    self.password = [[UILabel alloc] initWithFrame:CGRectMake(40 * kWidthFitParameter, self.userName.bottom +  30 * kWidthFitParameter, 80 * kWidthFitParameter, 30 * kWidthFitParameter)];
    self.password.text = @"密码";
    self.password.font = [UIFont systemFontOfSize:20 * kWidthFitParameter];
   // self.password.backgroundColor = [UIColor redColor];
    [self.view addSubview:_password];
    
    self.passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(self.password.right + 10 * kWidthFitParameter, self.password.top, 200 * kWidthFitParameter, 30 * kWidthFitParameter)];
   // self.passwordTF.backgroundColor = [UIColor redColor];
    self.passwordTF.borderStyle = UITextBorderStyleRoundedRect;
    self.passwordTF.placeholder = @"请输入密码";
    self.passwordTF.secureTextEntry = YES;
    [self.view addSubview:_passwordTF];
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    self.loginButton.frame = CGRectMake(80 * kWidthFitParameter, 250 * kWidthFitParameter, 70 * kWidthFitParameter, 30 * kWidthFitParameter);
    self.loginButton.layer.cornerRadius = 5 * kWidthFitParameter;
    self.loginButton.backgroundColor = [[UIColor brownColor] colorWithAlphaComponent:0.3];
    [self.loginButton addTarget:self action:@selector(actionToLogin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginButton];
    
    self.registButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.registButton setTitle:@"注册" forState:UIControlStateNormal];
    self.registButton.frame = CGRectMake(self.loginButton.right + 60 * kWidthFitParameter, 250 * kWidthFitParameter, 70 * kWidthFitParameter, 30 * kWidthFitParameter);
    self.registButton.layer.cornerRadius = 5 * kWidthFitParameter;
    self.registButton.backgroundColor = [[UIColor brownColor] colorWithAlphaComponent:0.3];
    [self.registButton addTarget:self action:@selector(actionToRegist:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registButton];

    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelRespone)];
    [self.view addGestureRecognizer:tapGes];
    
    
}

- (void)cancelRespone
{
    [self.userNameTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.userNameTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    return YES;
}

#pragma mark - 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark - 登陆
- (void)actionToLogin:(UIButton *)button
{
    [AVUser logInWithUsernameInBackground:self.userNameTF.text password:self.passwordTF.text block:^(AVUser *user, NSError *error) {
        NSInteger number = [[error.userInfo valueForKey:@"code"] integerValue];
        switch (number) {
            case 210:
            {
                [self showAlertControllerAndDismissWithTitle:@"提示" AndString:@"用户名或密码错误"];
                break;
            }
                case 211:
            {
                [self showAlertControllerAndDismissWithTitle:@"提示" AndString:@"用户名和密码不存在"];
            }
            default:
            {
                // 登陆成功
                [self showAlertControllerAndDismissWithTitle:@"提示" AndString:@"登录成功"];
                // NSLog(@"用户名：username = %@", user.username);
                [[UserManager shareUserManagerHandle] saveUserName:user.username];
                [[UserManager shareUserManagerHandle] saveIsLogined:@"YES"];
               
                break;
            }
        }
       
        
    }];
    
    
}

#pragma mark - 注册
- (void)actionToRegist:(UIButton *)button
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

#pragma mark - 封装一个方法弹出提示框并在2秒后消失

- (void)showAlertControllerAndDismissWithTitle:(NSString *)title AndString:(NSString *)alertString
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:alertString preferredStyle:UIAlertControllerStyleAlert];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alertVC dismissViewControllerAnimated:YES completion:nil];
         [self.navigationController popViewControllerAnimated:YES];
    });
    [self presentViewController:alertVC animated:YES completion:nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
